package org.lanqiao.tjut.action;

import java.util.List;

import javax.annotation.Resource;

import org.lanqiao.tjut.bean.UserInfoBean;
import org.lanqiao.tjut.service.IUserInfoService;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestFulServiceAction {
	@Resource
	private IUserInfoService userS;
	/**
	 * 查询所有用户信息
	 * @return json对象
	 */
	@RequestMapping(value = "/user/", method = RequestMethod.GET)
	public ResponseEntity<List<UserInfoBean>> selectAllUserInfo(){
		//调用dao层进行数据库的查询所有用户信息
		List<UserInfoBean> lstUserS = userS.selectAllUserInfo();
		if(lstUserS.isEmpty()){
			return new ResponseEntity<List<UserInfoBean>>(HttpStatus.NO_CONTENT);
		}
		return new ResponseEntity<List<UserInfoBean>>(lstUserS,HttpStatus.OK);
	}
	/**
	 * 通过ID查询用户信息
	 * @param sid
	 * @return json对象
	 */
	@RequestMapping(value = "/user/{sid}", method = RequestMethod.GET ,produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<UserInfoBean> selectUserInfo(@PathVariable("sid")Short sid){
		//调用dao层进行数据库的查询所有用户信息
		UserInfoBean User = userS.selectByPrimaryKey(sid);
		if(User == null){
			return new ResponseEntity<UserInfoBean>(HttpStatus.NO_CONTENT);
		}
		return new ResponseEntity<UserInfoBean>(User,HttpStatus.OK);
	}
	
	/**
	 * 添加用户信息
	 * @param userB
	 * @return json对象
	 */
	@RequestMapping(value = "/user/", method = RequestMethod.POST)
	public ResponseEntity<List<UserInfoBean>> saveUserInfo(@RequestBody UserInfoBean userB ){
		System.out.println("添加用户信息：" + userB.toString());
		//调用dao层进行数据库的查询所有用户信息
		List<UserInfoBean> lstUserS = userS.selectAllUserInfo();
		return new ResponseEntity<List<UserInfoBean>>(lstUserS,HttpStatus.OK);
	}
	/**
	 * 更新用户信息
	 * @param sid
	 * @param userB
	 * @return json对象
	 */
	@RequestMapping(value = "/user/{sid}", method = RequestMethod.PUT)
	public ResponseEntity<UserInfoBean> updatUserInfoById(@PathVariable("sid")Short sid ,@RequestBody UserInfoBean userB){
		System.out.println("更新用户信息的ID："+ sid);
		System.out.println("输出用户信息：" +userB.toString() );
		//调用dao层进行数据库的查询所有用户信息
		UserInfoBean User = userS.selectByPrimaryKey(sid);
		if(User == null){
			System.out.println("该用户不存在");
			return new ResponseEntity<UserInfoBean>(HttpStatus.NO_CONTENT);
		}
		return new ResponseEntity<UserInfoBean>(User,HttpStatus.OK);
	}
	/**
	 * 通过用户ID删除用户信息
	 * @param sid
	 * @return 用户ID
	 */
	@RequestMapping(value = "/user/{sid}", method = RequestMethod.DELETE)
	public ResponseEntity<String> deleteUserInfoById(@PathVariable("sid")String sid ){
		System.out.println("删除用户信息的ID："+ sid);
		
		//调用dao层进行数据库的查询所有用户信息
		int i = userS.deleteByPrimaryKey(sid);
		if(i == 0){
			System.out.println("该用户不存在或者删除失败");
			return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
		}
		return new ResponseEntity<String>("删除成功",HttpStatus.OK);
	}

}
